//
//  CCDetailModel.m
//  Party
//
//  Created by its on 15/3/30.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCDetailModel.h"
#import "AFHTTPRequestOperationManager+RACSupport.h"
#import <ReactiveMantle/RACSignal+Mantle.h>
#import "CCServer.h"

@interface CCDetailModel()
@property (nonatomic, copy) NSString *id;
@end
@implementation CCDetailModel
- (id) initWitActivityID:(NSString *)id {
  if (self = [super init]) {
    _id = id;
  }
  return self;
}

- (RACSignal *) requestDetail {
  RACSignal *signal =
  [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *queryURL = [NSString stringWithFormat:@"%@?id=%@",
                          CC_DETAIL_RUL,_id];
    RACDisposable *disposable =
    [[[[[manager rac_GET:queryURL parameters:nil] replayLazily]
       map:^id(RACTuple *value) {
         return value.first;
       }] parseResponseForClass:[CCDetailResult class]]
     subscribeNext:^(CCDetailResult *x) {
       [subscriber sendNext:x];
     }
     error:^(NSError *error) {
       [subscriber sendError:error];
     }
     completed:^{
       [subscriber sendCompleted];
     }];
    RACCompoundDisposable *compoundDisposable = [RACCompoundDisposable compoundDisposable];
    [compoundDisposable addDisposable:disposable];
    return compoundDisposable;
  }];
  return signal;
}
@end


@implementation CCDetailResult

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
  return @{
    @"error" : @"error",
//    @"userinfo" : @"data.author",
    @"address" : @"data.address",
    @"title" : @"data.title",
    @"money" : @"data.money",
    @"followCount" : @"data.followedCount",
    @"activityTime" : @"data.time",
    @"cotent" : @"data.description",
    @"applyCount" : @"data.people",
    @"images" : @"data.images",
    @"replayCount" : @"data.replyCount"
  };
}

+ (NSValueTransformer *) errorJSONTransformer {
  return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSDictionary *error) {
    return [NSError errorWithDomain:@"error" code:0 userInfo:error];
  }];
}

+ (NSValueTransformer *) authorJSONTransformer {
  return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSDictionary *dic) {
    return [MTLJSONAdapter modelOfClass:[CCUserInfo class] fromJSONDictionary:dic error:NULL];
  }];
}

+ (NSValueTransformer *) addressJSONTransformer {
  return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSDictionary *dic) {
    return [MTLJSONAdapter modelOfClass:[CCPartyAddress class] fromJSONDictionary:dic error:NULL];
  }];
}

@end